#include <iostream>
#include <vector>
using namespace std;

int n,root,vis[10],l[10],r[10],flag = 0;
vector<int> v,t;

void inOrder(int u){
    if(u==-1)    return;
    inOrder(l[u]);
    if(flag)    cout<<" ";
    cout<<u;
    flag = 1;
    inOrder(r[u]);
}

signed main()
{
    
    cin>>n;
    for(int i=0;i<10;i++){
        vis[i] = 0;
        l[i] = -1;
        r[i] = -1;
    }
    for(int i=0;i<n;i++){
        char a,b;
        cin>>a>>b;
        if(a!='-'){
            r[i] = a-'0';
            vis[r[i]] = 1;
        }
        if(b!='-'){
            l[i] = b-'0';
            vis[l[i]] = 1;
        }
    }
    for(int i=0;i<n;i++)    if(vis[i]==0)    root = i;
    v.push_back(root);
    cout<<root;
    while(!v.empty()){
        t.clear();
        for(int i=0;i<v.size();i++){
            if(l[v[i]]!=-1)    t.push_back(l[v[i]]);
            if(r[v[i]]!=-1)    t.push_back(r[v[i]]);
        }
        v=t;
        for(int i=0;i<v.size();i++){
            cout<<" "<<v[i];
        }
    }
    cout<<"\n";
    inOrder(root);
    cout<<"\n";
    return 0;
}
